
use processed\RepresentativeRPI,replace

rename *_REP REP_*

forvalues x = 1/17	 {
gen b`x' = REP_s_RPI_`x'
} 

keep b* datayear PC_per
rename PC_per I 
merge m:1 datayear using "processed\pricemerge_dis.dta"

reshape long b price, i(datayear) j(goods_id)

	xtset goods_id  datayear
	gen dlogp = log(price) - log(l.price)
	gen  dlogTOR_tmp = (1/2)*(l.b+b)*dlogp 
	bysort datayear : egen dlogTOR = total(dlogTOR_tmp)
	duplicates drop datayear,force

	gen dlog_TOR_CUM = 0 if datayear == 1974
		tset datayear
		forvalues tloop = 1975/2017 {
	replace dlog_TOR_CUM = l.dlog_TOR_CUM + dlogTOR if `tloop' == datayear
        } 	
	gen logpc =log(I) 
	gen logc = logpc - dlog_TOR_CUM
preserve
rename dlog_TOR_CUM log_TOR
keep log_TOR
outsheet using DataforMatlab\torn.csv, comma replace nonames
restore 
	
	
